package com.sander.guardmanagement.ui.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.sander.guardmanagement.R;
import com.sander.guardmanagement.http.BaseCallback;
import com.sander.guardmanagement.http.HomeDeleteUserHome;
import com.sander.guardmanagement.http.HomeGetRentList;
import com.sander.guardmanagement.http.OkHttpHelper;
import com.sander.guardmanagement.response.ResponseHomeGetRentList;
import com.sander.guardmanagement.response.ResponseHomeGetRentListData;
import com.sander.guardmanagement.response.ResponseHomeGetRuzhurenList;
import com.sander.guardmanagement.response.ResponseHomeGetUserHomeListData;
import com.sander.guardmanagement.response.ResponseModel;
import com.sander.guardmanagement.response.ResponseUserLoginData;
import com.sander.guardmanagement.until.UserInfoManager;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.util.ArrayList;
import java.util.List;

public class TenantManagementActivity extends AppCompatActivity {
    ResponseHomeGetUserHomeListData homedata;

    List<ResponseHomeGetRentListData> rentList = new ArrayList<>();
    tenantManagerAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tenant_management);

        Intent intent = getIntent();
        homedata = (ResponseHomeGetUserHomeListData) intent.getSerializableExtra("ruzhuren");
        TextView address = (TextView) findViewById(R.id.addresscontent);
        address.setText(homedata.getAdress());

        initCallBack();
        initData();
        initView();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

    }
    private void initCallBack(){
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.callbackLayout);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void initData()
    {
        HomeGetRentList homeGetRentList = new HomeGetRentList();
        homeGetRentList.uid = UserInfoManager.sharedManager().getUserInfo().getId() + "";
        homeGetRentList.home_id = homedata.id + "";
        OkHttpHelper.getinstance().post(homeGetRentList.requestUrl(), homeGetRentList.getValueOfClas(), new BaseCallback<ResponseHomeGetRentList>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseHomeGetRentList o) {
                if (o.getCode() == 0){
                    rentList = o.data;
                    Log.d("sander"," " + rentList.size());
                    updateadapterList();

                }
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });

    }
    public void deletedCity(final int postion){
        HomeDeleteUserHome deleteUserHome = new HomeDeleteUserHome();
        ResponseHomeGetRentListData data = rentList.get(postion);

        ResponseUserLoginData userLoginData = UserInfoManager.sharedManager().getUserInfo();

        deleteUserHome.uid = userLoginData.getId() + "";
        deleteUserHome.home_id = data.id + "";
        deleteUserHome.op_type = "1"; // 业主删除租客的信息

        OkHttpHelper.getinstance().post(deleteUserHome.requestUrl(), deleteUserHome.getValueOfClas(), new BaseCallback<ResponseModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onSuccess(Response response, ResponseModel o) {
                Log.d("sander","delete --> " + o.getMsg());
                if (o.getCode() == 0){
                    rentList.remove(postion);
                    updateadapterList();

                }else{
                    showToastMessage(o.getMsg());
                }

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });

    }


    public void updateadapterList(){
        adapter.notifyDataSetChanged();
    }
    public void initView()
    {
        ListView listView = (ListView) findViewById(R.id.tenantmanagerlist);
        adapter = new tenantManagerAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("sander", "selected  at " + position );
            }
        });
    }
    public void showAlertView(final int postion)
    {
        new MaterialDialog.Builder(this)
                .title("提示")
                .content("确定删除吗？")
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
                        deletedCity(postion);
                    }
                })
                .show();

        Log.d("sander","确定删除了吧");

    }

    public void showToastMessage(String message){
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();

    }
    private deletedTenantListClick deletetenant = new   deletedTenantListClick() {
        @Override
        public void onclickManager(int position, View view) {
            Log.d("sander","删除");

            showAlertView(position);

        }
    };
    private class tenantManagerAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return rentList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            tenantItem item = null;
            if (convertView == null){
                LayoutInflater infalter = getLayoutInflater();
                convertView =  infalter.inflate(R.layout.tenantitem,parent,false);
                item = new tenantItem();
                item.textView = (TextView) convertView.findViewById(R.id.tenenttextview);
                item.delete_button = (Button)convertView.findViewById(R.id.deleteactionbutton);
                convertView.setTag(item);
            }else{
                item = (tenantItem) convertView.getTag();
            }

            ResponseHomeGetRentListData data = rentList.get(position);


            item.textView.setText(data.getuserInfo());
            item.delete_button.setOnClickListener(deletetenant);
            item.delete_button.setTag(position);

            return convertView;
        }
    }
    private class tenantItem{
        TextView textView;
        Button delete_button;

    }
    public static abstract class deletedTenantListClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            onclickManager((Integer)v.getTag(),v);
        }
        public abstract void onclickManager(int  position, View view);
    }
}
